package hero.mps.web.school_class.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import hero.mps.utils.ResultUtils;
import hero.mps.utils.ResultVo;
import hero.mps.web.school_class.entity.ClassParam;
import hero.mps.web.school_class.entity.SchoolClass;
import hero.mps.web.school_class.service.SchoolClassService;
import hero.mps.web.school_major.entity.SchoolMajor;
import hero.mps.web.school_major.service.SchoolMajorService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/class")
@RequiredArgsConstructor
public class SchoolClassController {
    private final SchoolClassService schoolClassService;
    private final SchoolMajorService schoolMajorService;

    /**
     * 新增班级
     * @param schoolClass
     * @return
     */
    @PostMapping
    public ResultVo add(@RequestBody SchoolClass schoolClass){
        boolean save = schoolClassService.save(schoolClass);
        if (save) {
            return ResultUtils.success("新增班级成功");
        }else {
            return ResultUtils.error("新增班级失败");
        }
    }

    /**
     * 编辑班级
     * @param schoolClass
     * @return
     */
    @PutMapping
    public ResultVo edit(@RequestBody SchoolClass schoolClass){
        boolean updateById = schoolClassService.updateById(schoolClass);
        if (updateById) {
            return ResultUtils.success("新增班级成功");
        }else {
            return ResultUtils.error("新增班级失败");
        }
    }

    /**
     * 删除班级
     * @param classId
     * @return
     */
    @DeleteMapping("/{classId}")
    public ResultVo edit(@PathVariable("classId") Integer classId){
        boolean removeById = schoolClassService.removeById(classId);
        if (removeById) {
            return ResultUtils.success("删除班级成功");
        }else {
            return ResultUtils.error("删除班级失败");
        }
    }

    /**
     * 查询班级
     * @param classParam
     * @return
     */
    @GetMapping("/list")
    public ResultVo list(ClassParam classParam){
        IPage<SchoolClass> list = schoolClassService.getList(classParam);
        if (list != null) {
            return ResultUtils.success("查询成功", list);
        }else {
            return ResultUtils.error("查询失败");
        }
    }

    /**
     * 根据学院id查询专业列表
     * @param
     * @return
     */
    @GetMapping("/getMajorListById")
    public ResultVo listMajor(Integer collegeId){
        // 构造查询条件
        QueryWrapper<SchoolMajor> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SchoolMajor::getCollegeId, collegeId);
        List<SchoolMajor> list = schoolMajorService.list(queryWrapper);
        if (list != null) {
            return ResultUtils.success("查询成功", list);
        }else {
            return ResultUtils.error("查询失败");
        }
    }

    /**
     * 根据专业id获取学院id
     * @param majorId
     * @return
     */
    @GetMapping("/getCollegeId")
    public ResultVo getCollegeId(Integer majorId){
        SchoolMajor major = schoolMajorService.getById(majorId);
        if (major != null) {
            return ResultUtils.success("查询成功", major);
        }else {
            return ResultUtils.error("查询失败", major);
        }
    }

}
